package com.esd.device.controller;

import com.esd.common.pojo.R;
import com.esd.device.entity.DeviceType;
import com.esd.device.service.DeviceTypeService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("deviceType")
public class DeviceTypeController {
    @Autowired
    private DeviceTypeService deviceTypeService ;

    /**
     * 查询所有终端类型列表
     * @return 终端类型列表
     */
    @GetMapping("selectAll")
    public R selectAll(){
        return R.ok(deviceTypeService.list()) ;
    }

    /**
     * 添加终端类型
     * @param deviceType 实体
     * @return R
     */
    @PostMapping("addDeviceType")
    public R addDeviceType(@RequestBody DeviceType deviceType){
        return deviceTypeService.addDeviceType(deviceType) ;
    }

    /**
     * 修改终端类型
     * @param deviceType 实体
     * @return R
     */
    @PutMapping("updateDeviceType")
    public R updateDeviceType(@RequestBody DeviceType deviceType){
        return deviceTypeService.updateDeviceType(deviceType) ;
    }

    /**
     * 根据Id删除终端类型
     * @param deviceTypeId Id
     * @return R
     */
    @DeleteMapping("deleteDeviceType/{deviceTypeId}")
    public R deleteDeviceType(@PathVariable Long deviceTypeId){
        return deviceTypeService.deleteById(deviceTypeId) ;
    }

    /**
     * 根据Id查询终端类型
     * @param deviceTypeId Id
     * @return R
     */
    @GetMapping("getById/{deviceTypeId}")
    public R selectById(@PathVariable Long deviceTypeId){
        return deviceTypeService.selectById(deviceTypeId) ;
    }

    @Value("${esd.deviceTypes}")
    private String deviceTypesJson ;

    @GetMapping("getDeviceTypes")
    public List<Map<String , Object>> getDeviceTypes() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        List<Map<String , Object>> list = mapper.readValue(
                new File(deviceTypesJson),
                mapper.getTypeFactory().constructCollectionType(List.class, Map.class));
        return list;
    }
}
